package cn.ktp.auth.biz;

import java.util.List;

import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fescar.core.context.RootContext;
import com.alibaba.fescar.spring.annotation.GlobalTransactional;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import cn.ktp.auth.entity.User;
import cn.ktp.auth.ibiz.IAuthBiz;
import cn.ktp.auth.iservice.IAuthService;
import cn.ktp.demo.iservice.IDemoService;

@Service
public class AuthBiz implements IAuthBiz {
	@Reference(timeout = 4000)
	private IAuthService authService;
	@Reference
	private IDemoService demoService;

	@Override
	@GlobalTransactional
	public User addUser(User user) {
		System.out.println("开始全局事务，XID = " + RootContext.getXID());
		demoService.demo();
		if (1 == 1) {
			throw new RuntimeException("bbb");
		}
		return authService.addUser(user);
	}

	@Override
	public List<User> listUser() {
		return authService.listUser();
	}

	@Override
	@GlobalTransactional
	public String testTransaction() {
		return demoService.testTransaction();
	}

	@Override
	@HystrixCommand(fallbackMethod = "helloFallBack")
	public String hello() {
		return authService.hello();
	}

	public String helloFallBack(Throwable throwable) {
		return throwable.getMessage();
	}

	@Override
	public String testNacosValue() {
		return demoService.nacosValueDemo();
	}

}
